<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 17-11-3
 * Time: 下午3:12
 */

namespace App\Repositories\Portal;

use App\Models;
class CategoryRepository extends PortalRepository
{

    public function __construct()
    {
        parent::__construct();
        $this->model = new Models\Portal\Category();
    }

    private static $instance;

    /**
     * 实现单例
     */
    public static function getInstance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * 声明实例模型
     * @param array $filter
     * @return mixed
     */
    public function getSelectModel(array $filter = []) : \Illuminate\Database\Eloquent\Builder
    {
        $model = $this->model;

        $model = $this->getSiteIdModel($model, $filter);

        //  上级栏目编号
        if (isset($filter['pid'])) {
            $model = $model->where('pid', '=', (int) $filter['pid']);
        }

        //  栏目名称
        if (isset($filter['title'])) {
            $model = $model->where('title', 'like', '%'.$filter['title'].'%');
        }

        //  文档类型
        if (isset($filter['document_type'])) {
            if (is_array($filter['document_type'])) {
                $model = $model->whereIn('document_type', (array) $filter['document_type']);
            } else {
                $model = $model->where('document_type', '=', $filter['document_type']);
            }
        }

        //  是否时单页面
        if (isset($filter['is_page'])) {
            $model = $model->where('is_page', '=', (int) $filter['is_page']);
        }

        //  前台是否显示
        if (isset($filter['is_disabled'])) {
            $model = $model->where('is_disabled', '=', (int) $filter['is_disabled']);
        }

        //  发布权限
        if (isset($filter['create_auth'])) {
            $model = $model->where('create_auth', '=', (int) $filter['create_auth']);
        }

        $model = $model->where('deleted_at', null);

        return $model;
    }

    /**
     * 获取后台分页
     * @param null $siteId
     * @param null $pid
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getAdminPaginateData($pid = null, $siteId = null) : \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        return $this->getSelectModel([
            'pid' => $pid,
            'site_id' => $siteId
        ])->orderBy('id', 'asc')->paginate($this->pageSize);
    }

    /**
     * 获取指定条件的栏目列表
     * @param int $pid
     * @param int $isDisabled
     * @param null $isPage
     * @param null $siteId
     * @param null $documentType
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getCategoryList($pid = 0, $isDisabled = 0, $documentType = null, $isPage = null, $siteId = null)
    {
        return $this->getSelectModel([
            'pid' => $pid,
            'is_disabled' => $isDisabled,
            'is_page' => $isPage,
            'site_id' => $siteId,
            'document_type' => $documentType
        ])->orderBy('id', 'asc')->get();
    }

    /**
     * 获取不允许发布内容的栏目编号列表
     * @return \Illuminate\Support\Collection
     */
    public function getNotAllowContentIdList()
    {
        return $this->getSelectModel()->where('is_allow_content', '=', 0)->orWhere('is_page', '=', '1')->pluck('id');
    }

    /**
     * 获取单页面栏目
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getPageList()
    {
        return $this->getSelectModel()->where('is_page', '=', '1')->orWhere('document_type', '=', 'page')->get();
    }

    /**
     * 根据id获取子栏目的id列表
     * @param $id
     * @return \Illuminate\Support\Collection
     */
    public function getChildIdList($id)
    {
        return $this->getSelectModel(['pid' => $id])->pluck('id');
    }
}